package org.abratuhi.kvngroupware.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.BrainstormController;
import org.abratuhi.kvngroupware.model.Brainstorm;
import org.abratuhi.kvngroupware.spring.SpringUtilities;

@SuppressWarnings("serial")
public class GUIBrainstorm extends JPanel{
	public JMenuBar menubar = new JMenuBar();
	public JMenu menuAction = new JMenu("Action");
	public JMenuItem menuitemSwitch = new JMenuItem();
	public JMenuItem menuitemOverview = new JMenuItem();
	
	public JList listUsers = new JList(new String[0]);
	public JTextArea textareaSheet = new JTextArea();
	public JButton buttonCommit = new JButton();
	
	KVNGroupwareClient kvnclient;
	Brainstorm kvnbrainstorm;
	
	BrainstormController controller;
	
	JFrame frame;
	
	public GUIBrainstorm(KVNGroupwareClient kvnclient, Brainstorm brainstorm, Properties locale){
		super();
		
		this.buttonCommit.setText(locale.getProperty("brainstorm.button.commit_sheet"));
		this.menuAction.setText(locale.getProperty("brainstorm.menu.action"));
		this.menuitemOverview.setText(locale.getProperty("brainstorm.menuitem.view_brainstorm"));
		this.menuitemSwitch.setText(locale.getProperty("brainstorm.menuitem.switch_sheet"));

		this.kvnclient = kvnclient;
		this.kvnbrainstorm = brainstorm;
		this.controller = new BrainstormController(kvnclient, this, kvnbrainstorm);
		this.kvnbrainstorm.setGUI(this);
		
		listUsers.setPreferredSize(new Dimension(100, 200));

		textareaSheet.setRows(40);
		textareaSheet.setColumns(40);
		textareaSheet.setWrapStyleWord(true);
		textareaSheet.setLineWrap(true);
		
		buttonCommit.addActionListener(this.controller);
		menuitemSwitch.addActionListener(this.controller);
		menuitemOverview.addActionListener(this.controller);
		
		menubar.add(menuAction);
		menuAction.add(menuitemSwitch);
		menuAction.add(menuitemOverview);

		JPanel users = new JPanel(new SpringLayout());
		users.setBorder(new TitledBorder(locale.getProperty("brainstorm.border.users")));
		users.add(new JScrollPane(listUsers));
		SpringUtilities.makeCompactGrid(users, 1, 1, 0, 0, 0, 0);

		JPanel send = new JPanel(new SpringLayout());
		send.add(buttonCommit);
		SpringUtilities.makeCompactGrid(send, 1, 1, 0, 0, 0, 0);

		JPanel chat = new JPanel(new SpringLayout());
		chat.setBorder(new TitledBorder(locale.getProperty("brainstorm.border.sheet")));
		chat.add(new JScrollPane(textareaSheet));
		chat.add(send);
		SpringUtilities.makeCompactGrid(chat, 2, 1, 0, 0, 0, 0);

		setLayout(new SpringLayout());
		setVisible(true);
		add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, users, chat));
		SpringUtilities.makeCompactGrid(this, 1, 1, 0, 0, 0, 0);
		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		listUsers.setListData(kvnbrainstorm.getMemberIdsAsArray());
		textareaSheet.setText(kvnbrainstorm.getSheet(kvnclient.getUser().getId()));
		
		listUsers.repaint();
		textareaSheet.repaint();
	}
	
	public void createJFrame(){
		frame = new JFrame("..::KVNGroupware Client ::.."+"\t"+"Brainstorm: "+kvnbrainstorm.getId());
		frame.setJMenuBar(menubar);
		frame.getContentPane().add(this);
		frame.addWindowListener(this.controller);
		frame.setVisible(true);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}

}
